﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace ProceduralGolf.generation
{
    class Heightmap
    {
        private float[,] data;
        private int width;
        private int height;

        public int Width { get { return width; } }
        public int Height { get { return height; } }

        private Texture2D heightmapTex;
        public Texture2D HeightmapTex { get { return heightmapTex; } }

        public Heightmap(Texture2D heightmap_texture, float scale)
        {
            //this.heightmapTex = heightmap_texture;
            this.width = heightmap_texture.Width;
            this.height = heightmap_texture.Height;
            Color[] tex_data = new Color[Width * Height];
            heightmap_texture.GetData(tex_data);

            data = new float[Width, Height];

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    Color c = tex_data[y*Width + x];
                    data[x,y] = (float)(scale*((c.R + c.G + c.B) / 3.0f)/255.0f);
                }
            }
        }

        ~Heightmap()
        {
            if (heightmapTex != null)
                heightmapTex.Dispose();
        }

        public void setHeight(int x, int y, float height)
        {
            data[x, y] = height;
        }

        public Heightmap(float[,] data)
        {
            this.width = data.GetLength(0);
            this.height = data.GetLength(1);
            this.data = data;
            heightmapTex = null;
        }

        public float getHeight(int x, int y)
        {
            if (x < 0 || x >= Width) return 0.0f;
            if (y < 0 || y >= Height) return 0.0f;
            return data[x, y];
        }


    }
}
